package com.zbs.framework.pay.util;

import com.alibaba.fastjson.JSON;
import com.alipay.easysdk.payment.app.models.AlipayTradeAppPayResponse;
import com.alipay.easysdk.payment.common.models.AlipayTradeQueryResponse;
import com.alipay.easysdk.payment.common.models.AlipayTradeRefundResponse;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePayResponse;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.alipay.easysdk.payment.wap.models.AlipayTradeWapPayResponse;
import com.github.binarywang.utils.qrcode.MatrixToImageWriter;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.zbs.framework.pay.qrcode.QrCodeResponse;
import com.zbs.framework.pay.qrcode.QrResponse;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Hashtable;

public class QrUtils {

    public static QrCodeResponse qrcodePay(AlipayTradePagePayResponse response) {
        QrResponse qrResponse = JSON.parseObject(response.getBody(), QrResponse.class);
        return qrResponse.getAlipay_trade_precreate_response();
    }

    public static QrCodeResponse qrcodePay(AlipayTradeQueryResponse response) {
        QrResponse qrResponse = JSON.parseObject(response.getHttpBody(), QrResponse.class);
        return qrResponse.getAlipay_trade_precreate_response();
    }

    public static QrCodeResponse qrcodePay(AlipayTradeRefundResponse response) {
        return new QrCodeResponse().setCode(response.code).setMsg(response.subMsg).setOut_trade_no(response.outTradeNo);
    }

    public static QrCodeResponse qrcodePay(AlipayTradeWapPayResponse response) {
        QrResponse qrResponse = JSON.parseObject(response.getBody(), QrResponse.class);
        return qrResponse.getAlipay_trade_precreate_response();
    }

    public static QrCodeResponse qrcodePay(AlipayTradeAppPayResponse response) {
        QrResponse qrResponse = JSON.parseObject(response.getBody(), QrResponse.class);
        return qrResponse.getAlipay_trade_precreate_response();
    }

    public static QrCodeResponse qrcodePay(AlipayTradePrecreateResponse response) {
        QrResponse qrResponse = JSON.parseObject(response.getHttpBody(), QrResponse.class);
        return qrResponse.getAlipay_trade_precreate_response();
    }

    public static QrCodeResponse qrcodePay(AlipayTradePayResponse response) {
        QrResponse qrResponse = JSON.parseObject(response.getHttpBody(), QrResponse.class);
        return qrResponse.getAlipay_trade_precreate_response();
    }

    public static byte[] getQRCodeImage(String text, int width, int height) throws WriterException, IOException {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.MARGIN, 1);//去除多余白边
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints);
        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
        return pngOutputStream.toByteArray();
    }

    public static String getQRCodeImageBase64(byte[] bytes){
        String png_base64 = Base64.getEncoder().encodeToString(bytes).trim();//转换成base64串
        png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
        return "data:image/jpg;base64,"+png_base64;
    }


    public static String getQRCodeImageBase64(String text){
        String qrBase64 = null;
        try {
            byte[]a = getQRCodeImage(text, 350, 350);
            String png_base64 = Base64.getEncoder().encodeToString(a).trim();//转换成base64串
            png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
            qrBase64= "data:image/jpg;base64,"+png_base64;

        } catch (WriterException | IOException e) {
            e.printStackTrace();
        }

        return qrBase64;
    }
}